package com.zaoshui.hudada.scoring;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zaoshui.hudada.model.dto.question.QuestionContentDTO;
import com.zaoshui.hudada.model.entity.App;
import com.zaoshui.hudada.model.entity.Question;
import com.zaoshui.hudada.model.entity.ScoringResult;
import com.zaoshui.hudada.model.entity.UserAnswer;
import com.zaoshui.hudada.service.QuestionService;
import com.zaoshui.hudada.service.ScoringResultService;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
* 自定义测评评分策略
* */
@ScoringStrategyConfig(appType = 1,scoringStrategy = 0)
public class CustomTestScoringStrategy implements ScoringStrategy {

    @Resource
    QuestionService questionService;

    @Resource
    ScoringResultService scoringResultService;

    @Override
    public UserAnswer doScore(List<String> choices, App app) {
        // 1. 根据应用的id获取到题目和评分结果
        Long appId = app.getId();
        Question question = questionService.getOne(
                Wrappers.lambdaQuery(Question.class)
                        .eq(Question::getAppId, appId)
        );
        List<ScoringResult> scoringResultList = scoringResultService.list(
                Wrappers.lambdaQuery(ScoringResult.class)
                        .eq(ScoringResult::getAppId, appId)
        );

        // 2. 根据用户选择的答案获取到每个属性的分数
        Map<String,Integer> optionsCount = new HashMap<>();
        List<QuestionContentDTO> questionContentDTOList = JSONUtil.toList(question.getQuestionContent(), QuestionContentDTO.class);
        for (int i = 0; i < choices.size(); i++) {
            for (QuestionContentDTO.Option option : questionContentDTOList.get(i).getOptions()) {
                if(choices.get(i).equals(option.getKey())){
                    String result = option.getResult();
                    //哈希表中没有这个key
                    if(!optionsCount.containsKey(result)){
                        optionsCount.put(result,0);
                    }
                    optionsCount.put(result,optionsCount.get(result) + 1);
                }
            }
        }

        // 3. 根据每个属性的分数跟题目答案进行比较，获取到最佳的答案
        ScoringResult maxScoringResult = scoringResultList.get(0);
        int maxScore = 0;
        for (ScoringResult scoringResult : scoringResultList) {
            List<String> resultProp = JSONUtil.toList(scoringResult.getResultProp(), String.class);
            int score = resultProp.stream().mapToInt(prop -> optionsCount.getOrDefault(prop,0)).sum();
            if(score > maxScore){
                maxScore = score;
                maxScoringResult = scoringResult;
            }
        }


        // 4. 构造返回值，填充答案对象的属性
        UserAnswer userAnswer = new UserAnswer();
        userAnswer.setAppId(appId);
        userAnswer.setAppType(app.getAppType());
        userAnswer.setScoringStrategy(app.getScoringStrategy());
        userAnswer.setChoices(JSONUtil.toJsonStr(choices));
        userAnswer.setResultId(maxScoringResult.getId());
        userAnswer.setResultName(maxScoringResult.getResultName());
        userAnswer.setResultDesc(maxScoringResult.getResultDesc());
        userAnswer.setResultPicture(maxScoringResult.getResultPicture());
        return userAnswer;
    }
}
